<template>
  <div>
    <el-form ref="form" :rules="rules" :model="sysRole" label-width="110px" size="small">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="sysRole.roleName"  placeholder="请输入角色名称"></el-input>
      </el-form-item>
      <el-form-item label="排序码" prop="roleSort">
        <el-input v-model="sysRole.roleSort" placeholder="请输入排序码" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色备注" prop="remark">
        <el-input type="textarea" v-model="sysRole.remark" placeholder="请输入备注" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="sysRole.status">
          <el-radio v-for="item in statusOptions" :key="item.dictValue" :label="item.dictValue">{{item.dictLabel}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="small" @click="update">修改</el-button>
        <el-button type="warning" size="small" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import sysRoleApi from "@/api/system/sysRole";
export default {
  name: "sys-role-update",
  props:{
    activeId:{
      type:Number,
      default:undefined
    }
  },
  watch:{
    activeId:{
      immediate:true,
      handler:function (newVal,oldVal){
        this.getById(newVal)
      }
    }
  },
  data(){
    return{
      //保存的角色对象
      sysRole:{status:0},
      //角色状态的字典数据
      statusOptions: [],
      // 表单校验
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        roleSort: [
          { required: true, message: "角色排序码不能", trigger: "blur" }
        ]
      },
    }
  },
  created() {
    this.getDataByType("sys_status_type").then(res => {
      this.statusOptions = res.data;
    })
  },
  methods:{
    //根据ID查询角色数据
    getById(roleId){
      sysRoleApi.get(roleId).then(res=>{
        this.sysRole=res.data
      })
    },
    //重置
    resetForm(){
      this.getById(this.activeId);
    },
    //保存
    update(){
      //触发表单验证
      this.$refs["form"].validate(valid=>{
        if(valid){
          sysRoleApi.update(this.sysRole).then(res=>{
            this.$message.success(res.msg)
            this.$emit("after")
            this.$emit("close")
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
